define(['zepto', 'utils', 'JST'], function($, utils, JST){
  return {
    init: function(_CTX_, temp) {
      this._CTX_ = _CTX_
      this.temp = temp
      this.modal = {}
      
      this.$accountEdit = $('[href="#account-edit"]')
      this.$accountEdit.length ? this.bindPushAction() : void 0
    },

    bindPushAction: function() {

      var that = this

      // on account edit
      $(document).on('click', '[href="#account-edit"]', function(e) {
        e.preventDefault()
        var defer = that.compileaccountPage()

        $.when(defer).done(function($page) {
                $.push.transitionIn($page)
                
            })

      })

      // On account edited
      this.$accountEdit.on('edited:profile:account', function(e, $page, account, realName) {

        var itemDomString = $(JST.agentAccountTpl({
        	'account': account,
        	'realName': realName
        	}))

        if(that.temp) {
          $.toast("修改账户信息成功")
          setTimeout(function() {
                  window.location.reload()
                 }, 500)
        } else {
          $.push.transitionOut($page, {
            beforeTransition: function() {
              that.$accountEdit.off()
                .parent().empty().html(itemDomString)
              that.$accountEdit.parent().trigger("refresh")
            }
          })
        }
      })
    },

    compileaccountPage: function() {

      var that = this,
        deferPage = $.Deferred(),
        defer = this.requestaccount()

      $.when(defer).done(function(data) {

        var $page = $(JST.agentAccountTpl({
          'account': data.account,
          'realName': data.realName
        }))

        // Store request account
        that.modal.account = data.account;
        that.modal.realName = data.realName;
        
        // Bind submit
        that.bindaccountEditPageEvent($page)

        // Set defer as resolve status
        deferPage.resolve($page)
      })

      return deferPage.promise()
    },

    requestaccount: function() {
      var defer = $.Deferred(),
              url = this._CTX_.root + '/profile-online-agent/account'

          // Show request indicator
          $.showIndicator()

          // Request for rounds data
          $.ajax({
            'type': 'GET',
            'url': url,
            'cache': false
          }).done(function(response) {
            $.hideIndicator()  // Hide request indicator
            utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '获取账户信息失败')
            })
          })

        return defer.promise()
    },

    bindaccountEditPageEvent: function($page) {
      var that = this,
        $account = $('input[name="account"]', $page),
        $realName = $('input[name="realName"]', $page)
        
        $page.on('click', '#submit', function(e) {

          e.preventDefault()
          
          var flag = true;

          var account = $account.val().trim();
          var realName = $realName.val().trim();
          if(account == "" || realName == "") {
        	  flag = false;
        	  $.toast("支付宝账户和真实姓名不能为空");
          }
          
          if(flag) {
        	  var submiting = that.submitaccount(account, realName)
              $.when(submiting).done(function(data) {
                that.$accountEdit.trigger('edited:profile:account', [$page, that.account, that.realName])
              })
          }
          
        })
    },

    submitaccount: function(account, realName) {
      var that = this, defer = $.Deferred()
      $.showIndicator()
      $.post(that._CTX_.root + "/profile-online-agent/account", {
        'account': account,
        'realName': realName,
        '_method': 'PATCH'
      }).done(function(response) {
        $.hideIndicator()
              utils.processResult(response, function(result) {
                defer.resolve(result.data)
            }, function(result) {
                $.toast(result.message || '修改失败')
                defer.resolve(result.data)
            })
      })
    }
  }
})